package org.rocky.provider.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;

/**
 * @author Rocky
 * @date 2020/2/17 17:39
 */
@RequestMapping("/provider/delay")
@RestController
public class SendDelayController {
    private RabbitTemplate rabbitTemplate;


    @Autowired
    public SendDelayController( RabbitTemplate rabbitTemplate){
        this.rabbitTemplate= rabbitTemplate;
    }


    /**
     * 发送延迟消息
     * @param info  消息
     * @return 页面
     */
    @RequestMapping("/sendDelayMsg")
    public String sendDelayMsg(String info){
        String msg = "这是延迟消息 " + info;
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND,+4);
        Date delayTime = calendar.getTime();
        MessageProperties properties = new MessageProperties();
        properties.setHeader("x-delay", delayTime.getTime() - now.getTime());
        Message message = new Message(msg.getBytes(),properties);
        rabbitTemplate.send("DELAY_EXCHANGE","rocky.exchange",message);
        return "success";
    }

}
